package cn.bigfire.compiler.dragon.appendixA.handwrite.inter;

import cn.bigfire.compiler.dragon.appendixA.handwrite.symbols.Array;
import cn.bigfire.compiler.dragon.appendixA.handwrite.symbols.Type;

public class SetElem extends Stmt{

    public Id array;
    public Expr index;
    public Expr expr;

    public SetElem(Access access, Expr expr) {
        this.array = access.array;
        this.index = access.index;
        this.expr = expr;
        if (check(access.type, expr.type) == null) {
            error("type error");
        }
    }

    public Type check(Type type1, Type type2) {
        if (type1 instanceof Array || type2 instanceof Array) {
            return null;
        } else if (type1 == type2) {
            return type2;
        } else if (Type.numeric(type1) && Type.numeric(type2)) {
            return type2;
        } else return null;
    }

    public void gen(){
        String s1 = index.reduce().toString();
        String s2 = expr.reduce().toString();
        emit(array.toString() + "[" + s1 + "]=" + s2);
    }

}
